<?php
class mysql {
	private $link;
	public $encoding = 'utf8';
	public function __construct($host, $user, $password, $dbname) {
		if (! ($link = mysql_connect ( $host, $user, $password ))) {
			throw new Exception ( 'connect db error' );
		}
		$this->link = $link;
		if (! mysql_select_db ( $dbname, $link )) {
			throw new Exception ( 'select db error' );
		}
		try {
			$this->query ( 'SET NAMES ' . $this->encoding );
		} catch ( Exception $e ) {
			echo $e->getMessage ();
			exit ();
		}
	}
	public function findAll(Array $fields, $table, Array $params) {
		$queryString = 'SELECT ' . implode ( $fields, ', ' );
		$queryString .= " FROM `$table`";
		if (isset ( $params ['where'] )) {
			if (! empty ( $params ['where'] )) {
				
				foreach ( $params ['where'] as $where_key => $where_value ) {
					$temp_where [] = "`{$where_key}`='{$where_value}'";
				}
				$queryString .= ' WHERE ' . implode ( $temp_where, ' AND ' );
			}
		}
		if (isset ( $params ['order'] )) {
			if (! empty ( $params ['order'] )) {
				$queryString .= " ORDER BY {$params['order']}";
			}
		}
		if (isset ( $params ['limit'] )) {
			if (! empty ( $params ['limit'] )) {
				$queryString .= " LIMIT {$params['limit']['offset']}, {$params['limit']['length']}";
			}
		} else {
			$queryString .= " LIMIT 0, 10";
		}
		$rows = array ();
		try {
			$result = $this->query ( $queryString );
			while ( false !== ($row = $this->fetch ( $result )) ) {
				$rows [] = $row;
			}
		} catch ( Exception $e ) {
			echo $e->getMessage ();
			exit ();
		}
		return $rows;
	}
	public function update(Array $value, $table, Array $params) {
		if (! empty ( $value )) {
			foreach ( $value as $data_key => $data_value ) {
				$temp_data [] = "`{$data_key}`='{$data_value}'";
			}
			$queryString = "UPDATE {$table} SET " . implode ( $temp_data, ',' );
			if (isset ( $params ['where'] )) {
				if (! empty ( $params ['where'] )) {
					foreach ( $params ['where'] as $where_key => $where_value ) {
						$temp_where [] = "`{$where_key}`='{$where_value}'";
					}
					$queryString .= ' WHERE ' . implode ( $temp_where, ' AND ' );
				}
			}
			if (isset ( $params ['limit'] )) {
				if (! empty ( $params ['limit'] )) {
					$queryString .= " LIMIT {$params['limit']['length']}";
				}
			} else {
				$queryString .= " LIMIT 1";
			}
			try {
				$result = $this->query ( $queryString );
				return $result;
			} catch ( Exception $e ) {
				echo $e->getMessage ();
				exit ();
			}
		}
	}
	public function insert(Array $value, $table) {
		if (! empty ( $value )) {
			$insertKeys = array_keys ( $value );
			$insertValues = array_values ( $value );
			$queryString = "INSERT INTO {$table} (`" . implode ( $insertKeys, '`, `' ) . "`) VALUES ('" . implode ( $insertValues, "', '" ) . "')";
			try {
				$result = $this->query ( $queryString );
				return mysql_insert_id ( $this->link );
			} catch ( Exception $e ) {
				echo $e->getMessage ();
				exit ();
			}
		} else {
			throw new Exception ( 'insertvalue empty' );
		}
	}
	public function delete($table, Array $params) {
		$queryString = "DELETE FROM {$table} ";
		if (isset ( $params ['where'] )) {
			if (! empty ( $params ['where'] )) {
				foreach ( $params ['where'] as $where_key => $where_value ) {
					$temp_where [] = "`{$where_key}`='{$where_value}'";
				}
				$queryString .= ' WHERE ' . implode ( $temp_where, ' AND ' );
			}
		}
		if (isset ( $params ['order'] )) {
			if (! empty ( $params ['order'] )) {
				$queryString .= " ORDER BY {$params['order']}";
			}
		}
		if (isset ( $params ['limit'] )) {
			if (! empty ( $params ['limit'] )) {
				$queryString .= " LIMIT {$params['limit']['length']}";
			}
		} else {
			$queryString .= " LIMIT 1";
		}
		try {
			$result = $this->query ( $queryString );
		} catch ( Exception $e ) {
			echo $e->getMessage ();
			exit ();
		}
		return $result;
	}
	public function query($queryString) {
		$result = mysql_query ( $queryString, $this->link );
		if ($result === false) {
			throw new Exception ( 'query error:' . $queryString );
		}
		return $result;
	}
	public function fetch($result) {
		$row = mysql_fetch_array ( $result, MYSQL_ASSOC );
		return $row;
	}
}
/**
$db = new mysql ( '10.7.12.20', 'firoyang', 'firoyang', 'csknow' );
$params = array ( 
		'limit' => array ( 
				'offset' => 0, 
				'length' => 5 
		) 
);
$a = $db->findAll ( array ( 
		'*' 
), 'Trouble', $params );
$value = array ( 
		'Title' => 'test' 
);
$b = $db->insert ( $value, 'Trouble', $params );
$value = array ( 
		'Title' => 'test111' 
);
$params = array ( 
		'where' => array ( 
				'TroubleInfoID' => $b 
		) 
);
$c = $db->update ( $value, 'Trouble', $params );


$d = $db->delete ('Trouble', $params);
*/